﻿/**
* jQuery EasyUI 1.4.3
* Copyright (c) 2009-2015 www.jeasyui.com. All rights reserved.
*
* Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
* To use it on other terms please contact us at info@jeasyui.com
* http://www.jeasyui.com/license_commercial.php
*
* jQuery EasyUI datagrid 组件扩展
* jeasyui.extensions.datagrid.rowState.js
* 开发 流云
* 由 落阳 整理
* 最近更新：2015-11-24
*
* 依赖项：
*   1、jquery.jdirk.js
*
* Copyright (c) 2015 ChenJianwei personal All rights reserved.
*/
(function () {

    $.util.namespace("$.fn.datagrid.extensions");


    var autoMerge = function (target, fields) {
        var t = $(target);
        if (!fields) { fields = t.datagrid("getColumnFields"); }
        var rows = t.datagrid("getRows");
        var i = 0, j = 0, temp = {};
        for (i; i < rows.length; i++) {
            var row = rows[i];
            j = 0;
            for (j; j < fields.length; j++) {
                var field = fields[j];
                var tf = temp[field];
                if (!tf) {
                    tf = temp[field] = {};
                    tf[row[field]] = [i];
                } else {
                    var tfv = tf[row[field]];
                    if (tfv) {
                        tfv.push(i);
                    } else {
                        tfv = tf[row[field]] = [i];
                    }
                }
            }
        }
        $.each(temp, function (field, colunm) {
            $.each(colunm, function () {
                var group = this;

                if (group.length > 1) {
                    var before,
                    after,
                    megerIndex = group[0];
                    for (var i = 0; i < group.length; i++) {
                        before = group[i];
                        after = group[i + 1];
                        if (after && (after - before) == 1) {
                            continue;
                        }
                        var rowspan = before - megerIndex + 1;
                        if (rowspan > 1) {
                            t.datagrid('mergeCells', {
                                index: megerIndex,
                                type: "body",
                                field: field,
                                rowspan: rowspan
                            });
                        }
                        if (after && (after - before) != 1) {
                            megerIndex = after;
                        }
                    }
                }
            });
        });
    };



    var methods = $.fn.datagrid.extensions.methods = {

        //  扩展 easyui-datagrid 的自定义方法；判断指定的 data-row(数据行) 是否被 check；该方法的参数 index 表示要判断的行的索引号，从 0 开始计数；
        //  返回值：如果参数 index 所表示的 data-row(数据行) 被 check，则返回 true，否则返回 false。
        autoMergeCells: function (jq, fields) { return jq.each(function () { autoMerge(this, fields); }); }
    };

    var defaults = $.fn.datagrid.extensions.defaults = {

    };

    $.extend($.fn.datagrid.defaults, defaults);
    $.extend($.fn.datagrid.methods, methods);

})(jQuery);